package sam.learn.demo.kafka;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sam.learn.framework.web.ResponseResult;

@RestController
@RequestMapping("/demo/kafka")
public class KafkaController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping(value = "/send/notice")
    public ResponseResult<String> sendKafkaNotice(String message) {
        try {
            kafkaTemplate.send("notice", "key", message);
            logger.info("发送kafka成功.");
            return ResponseResult.success("ok");
        } catch (Exception e) {
            logger.error("发送kafka失败", e);
            return ResponseResult.fail("error");
        }
    }
}
